package com.china.osourcechina;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.china.adapter.Finduser_Adapter;
import com.china.base.BaseActivity;
import com.china.utils.HttpUtils;
import com.china.vo.Finduser;
import com.china.vo.User;
import com.thoughtworks.xstream.XStream;

import java.util.ArrayList;

/**
 * Created by jinyaoguang on 2016/5/10.
 */
public class Finduser_Activity extends BaseActivity{
    ListView lv_finduser;
    ImageView iv_back,iv_finduser_cancel;
    EditText et_finduser_search;
    ArrayList<User> list;
    String strs;
    Handler h=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Finduser_Adapter adapter=new Finduser_Adapter(list,Finduser_Activity.this);
            lv_finduser.setAdapter(adapter);
        }
    };

    @Override
    protected int loadWindowLayout() {
        return R.layout.finduser;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected void initViews() {

        lv_finduser = (ListView) findViewById(R.id.lv_finduser);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_finduser_cancel = (ImageView) findViewById(R.id.iv_finduser_cancel);
        et_finduser_search = (EditText) findViewById(R.id.et_finduser_search);
        initData();
    }

    private void initData() {
        list = new ArrayList<User>();
        et_finduser_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                iv_finduser_cancel.setVisibility(View.VISIBLE);
                String str=HttpUtils.get("http://www.oschina.net/action/api/find_user?name=" +s);

                //实例化Xstream
                XStream x = new XStream();
                //解析对象
                x.processAnnotations(Finduser.class);
                //实例化对象
                Finduser os = new Finduser();
                //得到解析数据
                os = (Finduser) x.fromXML(str);
                //清除集合
                list.clear();
                //得到集合
                list = os.getUsers().getUser();

                Log.i("aaaaa",list.toString());
                h.sendEmptyMessage(0);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        iv_finduser_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_finduser_search.setText("");
                iv_finduser_cancel.setVisibility(View.GONE);
            }
        });

        }
    }

